package cn.tedu.sp03.user.service;

import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sp01.pojo.Item;
import sp01.pojo.User;
import sp01.service.UserService;
import web.util.JsonUtil;

import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Value("${sp.user-service.users}")
    private String userJson;
    @Override
    public User getUser(Integer id) {
        List<User> items= JsonUtil.from(userJson, new TypeReference<List<User>>() {});
        if(Math.random()<0.9) {

            long t = new Random().nextInt(5000);
            log.info("item-service-+port+ - 暂停 "+t);
            try {
                Thread.sleep(t);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        for (User user:items) {
            if (user.getId().equals(id)){
                return user;
            }
        }
        return null;
    }

    @Override
    public void addScore(Integer id, Integer score) {

        log.info("user "+id+" - 增加积分 "+score);
    }
}
